<?php

namespace app\admin\controller\business;

use app\admin\model\SystemAdmin;
use app\common\controller\AdminController;
use app\common\model\BusinessDistrict;
use app\common\model\Shop;
use app\common\model\User;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="商圈管理员")
 */
class DistrictManage extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\User();

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $districtId = input('district_id', 0, 'intval');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where, $extra) = $this->buildTableParames([]);

            $count = $this->model->where('district_id', $districtId)
                ->where($where)
                ->count();
            $list = $this->model->where('district_id', $districtId)
                ->where($where)
                ->page($page, $limit)
                ->order('role asc,id desc')
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        $this->assign('districtId', $districtId);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        $districtId = input('district_id', 0, 'intval');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'phone|用户手机号' => 'requireWithout:user_id',
                'user_id|用户ID' => 'requireWithout:phone'
            ];
            $this->validate($post, $rule);
            if($post['user_id']){
                $user = User::where([
                    'id'=>$post['user_id'],
                    'status' => 1
                ])->find();
            }else{
                $user = User::where([
                    'phone'=>$post['phone'],
                    'status' => 1
                ])->find();
            }

            if (!$user) {
                $this->error('用户不存在或者已被禁用');
            }
            if ($user['district_id']) {
                $this->error('用户已关联其他商圈');
            }
            if ($user['shop_id']) {
                $this->error('用户已关联其他店铺');
            }
            if (Shop::where('user_id', $post['user_id'])->find()) {
                $this->error('用户已关联其他店铺~');
            }

            Db::startTrans();
            try {
                $user->district_id = $districtId;
                $user->save();
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }
        $this->assign('districtId', $districtId);
        return $this->fetch();
    }

    public function edit($id)
    {

    }

    public function export($id)
    {

    }

    public function modify($id)
    {

    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

}